package com.hugh.glrpcprovider.impl;


import com.hugh.entity.ResumeEntity;
import com.hugh.glrpcprovider.dao.ResumeDao;
import com.hugh.glrpcprovider.dao.pojo.Resume;
import com.hugh.service.ResumeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author gule
 */
@Service(value = "com.hugh.service.ResumeService")
public class ResumeServiceImpl implements ResumeService {

    @Autowired
    private ResumeDao resumeDao;

    @Override
    public List<ResumeEntity> queryResumeList() throws Exception {
        List<Resume> resumeList = resumeDao.findAll();
        if(CollectionUtils.isEmpty(resumeList)){
            return Collections.emptyList();
        }
        List<ResumeEntity> resList = new ArrayList<>(resumeList.size());
        for(Resume resume : resumeList){
            ResumeEntity resumeEntity = new ResumeEntity();
            BeanUtils.copyProperties(resume, resumeEntity);
            resList.add(resumeEntity);
        }
        return resList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addResume(ResumeEntity requesrResume) throws Exception {
        if(Objects.isNull(requesrResume)){
            return Boolean.TRUE;
        }
        Resume resume = new Resume();
        resume.setName(requesrResume.getName());
        resume.setAddress(requesrResume.getAddress());
        resume.setPhone(requesrResume.getPhone());
        resumeDao.save(resume);
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyResume(ResumeEntity requesrResume) throws Exception {
        if(Objects.isNull(requesrResume) || Objects.isNull(requesrResume.getId())){
            throw new Exception("modify data failed");
        }
        Resume resume = new Resume();
        resume.setId(requesrResume.getId());
        resume.setName(requesrResume.getName());
        resume.setAddress(requesrResume.getAddress());
        resume.setPhone(requesrResume.getPhone());
        resumeDao.save(resume);
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteResume(ResumeEntity resume) throws Exception {
        if(Objects.isNull(resume) || Objects.isNull(resume.getId())){
            throw new Exception("data delete failed");
        }
        resumeDao.deleteById(resume.getId());
        return Boolean.TRUE;
    }

    @Override
    public ResumeEntity getResumeById(Long id) throws Exception {
        Optional<Resume> byId = resumeDao.findById(id);
        if(byId.isPresent()){
            ResumeEntity resumeEntity = new ResumeEntity();
            BeanUtils.copyProperties(byId.get(), resumeEntity);
            return resumeEntity;
        }
        return null;
    }
}
